package com.qf.driver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qf.common.bean.R;
import com.qf.common.dto.DeviceTopicDto;
import com.qf.common.model.QfDeviceTopic;
import com.qf.driver.feign.DeviceTopicFeign;
import com.qf.driver.mqtt.bean.MqttProperties;
import com.qf.driver.service.DeviceTopicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 千锋健哥
 */
@Service
@Slf4j
public class DeviceTopicServiceImpl implements DeviceTopicService, BeanFactoryAware {

    @Autowired
    private DeviceTopicFeign deviceTopicFeign;

    //从spring核心中获取Spring的Bean工厂对象
    private BeanFactory beanFactory;

    @Autowired
    private MqttProperties mqttProperties;

    @Override
    public void deviceTopicJoin(String deviceKey) {
        //1. 根据deviceKey查询topic列表
        DeviceTopicDto deviceTopicDto = new DeviceTopicDto();
        deviceTopicDto.setDeviceKey(deviceKey);
        R<List> topicResult = deviceTopicFeign.findAll(deviceTopicDto);

        //2. 判断deviceTopic列表不为空, 如果为空发出警告
        if (topicResult == null || topicResult.getData().size() == 0) {
            throw new RuntimeException("deviceKey:" + deviceKey + ", 设备topic为空, 无法进行mqtt连接! 请检查tiopic表数据!");
        }

        //3. 获取mqttconfig初始化类中的主题订阅对象, 此对象无法注入, 所以需要手动获取
        //从spring容器的Bean工厂中获取这个对象
        Object mqttInbound = beanFactory.getBean("mqttInbound");
        //将mqttInbound对象转换成mqtt连接topic主题的对象
        MqttPahoMessageDrivenChannelAdapter mqtt = (MqttPahoMessageDrivenChannelAdapter)mqttInbound;

        //4. 完整新创建设备topic的连接, 进行增量连接topic
        if (topicResult != null) {
            for (Object obj : topicResult.getData()) {
                try {
                    QfDeviceTopic topic = BeanUtil.toBean(obj, QfDeviceTopic.class);
                    //将数据库中获取到的topic主题名称, 加入mqtt连接对象中, 进行平台与emqx服务器连接
                    mqtt.addTopic(topic.getTopic(), mqttProperties.getQos());
                } catch (Exception e) {
                    log.info("DeviceTopicServiceImpl类, 进行设备增量topic连接异常, 设备key:" + deviceKey);
                }
            }
        }
    }

    @Override
    public List<QfDeviceTopic> findTopicAll() {
        R<List> result = deviceTopicFeign.findAll(new DeviceTopicDto());
        List<QfDeviceTopic> list = new ArrayList<>();
        if (result.getData() != null) {
            for (Object obj : result.getData()) {
                list.add(BeanUtil.toBean(obj, QfDeviceTopic.class));
            }
        }
        return list;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

}
